<?php
ini_set('display_errors', 1);
ini_set("gd.jpeg_ignore_warning", 1);
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url;
use Phalcon\Mvc\Application;
use Phalcon\DI\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\Router;
use Phalcon\Config\Adapter\Ini as ConfigIni;
try {
	define('DEBUG', TRUE);
    define('APP_PATH', realpath('..') . DIRECTORY_SEPARATOR);
	require(APP_PATH . 'app' . DIRECTORY_SEPARATOR . 'vendor/autoload.php');
    $config = new ConfigIni(APP_PATH . 'app/configs/config.ini');
        // Register an autoloader
    $loader = new Loader();
    $loader->registerDirs(array(
        APP_PATH . $config->application->controllersDir,
        APP_PATH . $config->application->modelsDir,
        APP_PATH . $config->application->libsDir,
        APP_PATH . $config->application->configsDir,
        ))->registerNamespaces(array(
        'App\Controllers' => APP_PATH . $config->application->controllersDir,
        'App\Models' => APP_PATH . $config->application->modelsDir,
        'App\Libs' => APP_PATH . $config->application->libsDir,
        'App\Configs' => APP_PATH . $config->application->configsDir,
        ));
        $eventsManager = new \Phalcon\Events\Manager();

        $eventsManager->attach('loader', function ($event, $loader) {
            if ($event->getType() == 'beforeCheckPath') {
        //echo $loader->getCheckedPath(), "<br>";
            }
        });

        $loader->setEventsManager($eventsManager);

        $loader->register();

        // Create a DI
        $di = new FactoryDefault();


        $di->set('dispatcher', function() {
            $dispatcher = new \App\Libs\Base\BaseDispatcher();
            return $dispatcher;
        });

        $di->set('view', function(){
            $view = new View();
            $view->setViewsDir('../app/views/');
            return $view;
        });

        $di->set('url', function(){
            $url = new UrlProvider();
            $url->setBaseUri($config->application->baseUri);
            return $url;
        });
		
		$di->set('dbMaster', function(){
			return \App\Libs\DB\PailePdo::getConn('MASTER');
        });

		$di->set('dbSlave', function(){
			return \App\Libs\DB\PailePdo::getConn('SLAVE');
        });

        $di->set('router', function() {
            $router = new Router();
            $router->add('/', array(
                'controller' => 'home',
                'action'     => 'select.html'
                ));
            $router->add('/index/about.html', array(
                'controller' => 'index',
                'action'     => 'about.html'
                ));
            return $router;
        });

        $di->set('session', function() {
            static $session = FALSE;
            if ($session !== FALSE) {
                return $session;
            }
            $session = \App\Libs\Session\Session::load();
            return $session;
        });

        // set config
        \App\Libs\Base\Config::instance();
        // Handle the request
        $application = new Application($di);

        echo $application->handle()->getContent();

    } catch(\Exception $e) {
        echo "PhalconException: ", $e->getMessage();
    }
